package com.suirich.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suirich.bean.AboutUs;
import com.suirich.common.Constants;
import com.suirich.service.AboutUsService;

@Controller
@RequestMapping("/about")
public class AboutUsController {
	@Autowired
	private AboutUsService aboutUsService;
	
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,String> modify(AboutUs originalInfo) {
        Map<String,String> result = new HashMap<String,String>();
        
        try {
            AboutUs aboutUs = this.aboutUsService.read(1L);
            
            if(aboutUs == null){
                //解析并设置描述信息
                String contentInfo = originalInfo.getContentInfo();
                byte[] content = " ".getBytes();
                try {
                    if(contentInfo != null && contentInfo.length() > 0){
                    	content = contentInfo.getBytes("UTF-8");
                    	originalInfo.setContent(content);
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new UnsupportedEncodingException("字符串转字节数组异常！");
                }
                
                this.aboutUsService.create(originalInfo);
            }else{
                //解析并设置描述信息
                String contentInfo = originalInfo.getContentInfo();
                byte[] content = " ".getBytes();
                try {
                    if(contentInfo != null && contentInfo.length() > 0){
                    	content = contentInfo.getBytes("UTF-8");
                    	aboutUs.setContent(content);
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new UnsupportedEncodingException("字符串转字节数组异常！");
                }
                aboutUs.setImagePath(originalInfo.getImagePath());
                
                this.aboutUsService.update(aboutUs);
            }
            
            result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        
        return result;
    }

    @RequestMapping(value="/detail",method=RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> show(){

        Map<String,Object> result = new HashMap<String,Object>();
        
        try {
        	AboutUs aboutUs = this.aboutUsService.read(1L);
        	if(aboutUs != null){
            	aboutUs.setContent(null);
        	}
        	
        	result.put("aboutUs", aboutUs);
        	result.put("result", Constants.SUCCESS_RESULT_FLAG);
        } catch (Exception e) {
            
            result.put("result", Constants.FAILURE_RESULT_FLAG);
        }
        

        return result;
    }

}
